package crm.utility;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import crm.dbhandelling.Connections;






@SuppressWarnings("unused")
public class ViewUtilites {
	
	
	
	public static final Connections dbe=new Connections();
	//public static StartFrm sf;
	
	
	// takes string and gives java.sql.date
	
	public static java.sql.Date convert (String temp) throws Exception
	{
		 SimpleDateFormat sdf = null;
		
		if(temp.contains("-"))
		   sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(temp.contains("/"))
			sdf=new SimpleDateFormat("MM/dd/yyyy");
		java.util.Date date = null;
		date = sdf.parse(temp);
		java.sql.Date dates = new java.sql.Date(date.getTime());
		return dates;
	}
	
	
	// takes list and string aray and sets the list
	public static void setList(JList list, String[] data)
	{
		DefaultListModel dl=(DefaultListModel) list.getModel();
		for(int i=0;i<data.length;i++)
		{
			dl.addElement(data[i]);
		}
	}
	
	
	// takes table and returns the corresponding data as a 2d aray
	
	public static String[][] ret_data(JTable tab)
	{		DefaultTableModel dm=(DefaultTableModel) tab.getModel();
	        int rowcount=dm.getRowCount();
	        int colcount=dm.getColumnCount();
	        String[][] rd=new String[rowcount][colcount];
	        for(int i=0;i<rowcount;i++)	        	
	        {
	        	for(int j=0;j<colcount;j++)
	        	{
	        		rd[i][j]= dm.getValueAt(i,j)+"";
	        	}
	        }
		return rd;
	}
	
	


public boolean   date_val(String temp)
{

	java.util.Date date = null;
	SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
	
	try {
		
		date = sdf.parse(temp);
	} catch (ParseException e) {
		
		e.printStackTrace();
		return false;
	}
	return true;
}

// sets the combo box;

public static void set_comboBox(JComboBox com,String[]items)
{
	int itemCount = com.getItemCount();

    for(int i=itemCount-1;i>-1;i--){
        com.removeItemAt(i);
     }
		for(int i=0;i<items.length;i++)
		{
			com.addItem(items[i]);
		}
		
		System.out.println(com.getMaximumRowCount()+"row");
		com.revalidate();
		com.repaint();
}

// Methods takes a jlabel or jtext and sets the date funvtionality on them
public static void makeDateComponent(Object object)
{
	
	JLabel label;
	JTextField jtext;
	
	if(object instanceof JLabel)
	{
		label=(JLabel) object;
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar  rightNow = Calendar.getInstance();
		DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
		rightNow = myDialog.getAnswer();
		label.setText(sdf.format(rightNow.getTime()));
		myDialog.dispose();
	}
	
	else if(object instanceof JTextField)
	{
		
		jtext=(JTextField) object;;
		SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
		Calendar  rightNow = Calendar.getInstance();
		DatePicker myDialog = new DatePicker(new JFrame(), true, rightNow);
		rightNow = myDialog.getAnswer();
		jtext.setText(sdf.format(rightNow.getTime()));
		myDialog.dispose();
	}
	
	
	
	
}

public static Timestamp getTime()
{
	java.util.Date d=new java.util.Date();
	Timestamp t=new Timestamp(d.getTime());
	return t;
}








}
